package com.caigou.modules.rank.adapter

import android.text.TextUtils
import android.view.View
import android.widget.Button
import com.caigou.comm.base.utils.ImageLoader
import com.caigou.modules.rank.R
import com.caigou.modules.rank.bean.LevelRankItemBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * @author #Suyghur.
 * Created on 2021/11/06
 */
class LevelRankItemAdapter(dataset: MutableList<LevelRankItemBean>) :
    BaseQuickAdapter<LevelRankItemBean, BaseViewHolder>(R.layout.item_level_rank_list, dataset) {

    init {
        addChildClickViewIds(R.id.item_level_rank_iv_avatar)
        addChildClickViewIds(R.id.item_level_rank_btn_follow)
    }

    override fun convert(holder: BaseViewHolder, item: LevelRankItemBean) {
        if (!TextUtils.isEmpty(item.headImg)) {
            ImageLoader.loadCircle(context, item.headImg, holder.getView(R.id.item_level_rank_iv_avatar))
        } else {
            holder.setImageResource(R.id.item_level_rank_iv_avatar, R.drawable.img_comm_avatar)
        }

        val rank = if ((getItemPosition(item) + 4) <= 9) {
            "${getItemPosition(item) + 4} "
        } else {
            "${getItemPosition(item) + 4}"
        }

        holder.setText(R.id.item_level_rank_tv_index, rank)
        holder.setText(R.id.item_level_rank_tv_nickname, item.nickName)
        holder.setText(R.id.item_level_rank_tv_num, item.growthValue.toString())

        val btnFollow = holder.getView<Button>(R.id.item_level_rank_btn_follow)
        if (item.concern) {
            btnFollow.setBackgroundResource(R.drawable.comm_btn_gray2)
            btnFollow.text = "已关注"
        } else {
            btnFollow.setBackgroundResource(R.drawable.comm_btn_red2)
            btnFollow.text = "+关注"
        }

        if (getItemPosition(item) == itemCount - 1) {
            holder.getView<View>(R.id.item_level_rank_divider).visibility = View.GONE
        }
    }
}